//
//  NSObject+NEDDealloc.m
//  DZNEmptyDataSet
//
//  Created by 98du on 2021/12/16.
//

#import "NSObject+NEDDealloc.h"
#import <objc/runtime.h>

@interface __NEDAssocicatDeallocTempObect : NSObject
@property (nonatomic, copy) void(^deallocBlock)(Class cname);
@property (nonatomic, assign) Class linkClass;
@end

@implementation __NEDAssocicatDeallocTempObect

+ (instancetype)objWithDeallocBlock:(void(^)(Class cname))deallocBlock linkClass:(Class)cl{
    __NEDAssocicatDeallocTempObect * temp = [__NEDAssocicatDeallocTempObect new];
    temp.deallocBlock = deallocBlock;
    temp.linkClass = cl;
    return temp;
}

- (void)dealloc{
    !self.deallocBlock?:self.deallocBlock(self.linkClass);
}

@end


@implementation NSObject (NEDDealloc)

- (void)ned_deallocBlock:(void(^)(Class _Nullable cname))block{
    __NEDAssocicatDeallocTempObect * tmpObj = [__NEDAssocicatDeallocTempObect objWithDeallocBlock:block linkClass:self.class];
    objc_setAssociatedObject(self, _cmd, tmpObj, OBJC_ASSOCIATION_RETAIN);
}

@end
